package com.amkai.aes.webservice.binary.objecthandler.inventory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.amkai.aes.aescommon.inventory.InventorySearchParam;
import com.amkai.aes.aescommon.service.binary.job.Job;
import com.amkai.aes.aescommon.service.binary.job.inventory.InventoryJob;
import com.amkai.aes.aescommon.service.binary.job.inventory.InventoryJobKind;
import com.amkai.aes.aescommon.service.binary.job.inventory.PurchaseOrderJob;
import com.amkai.aes.aescommon.service.binary.job.inventory.PurchaseOrderLinesJob;
import com.amkai.aes.generatedbeans.inventory.VendorOrderInformation;
import com.amkai.aes.generatedbeans.inventory.purchasing.PurchaseOrder;
import com.amkai.aes.generatedbeans.inventory.purchasing.PurchaseOrderLine;
import com.amkai.aes.generatedbeans.inventory.purchasing.ReceiveOrderLine;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class PurchaseOrderLinesHandler implements JobObjectHandler<PurchaseOrderLinesJob,Serializable, ArrayList> {

	private static final String query = "select pol from PurchaseOrderLine pol where pol.purchaseOrder.id = :poId";
	private static final String polquery = "select pol from PurchaseOrderLine pol where pol.id = :poId";
	@Override
	public ArrayList process(PurchaseOrderLinesJob job, Serializable object)
			throws Exception {
		SessionManager sm = SiteManager.Start();
		try {
			PurchaseOrderLine pol;
			sm.openSession();
			Query q;
			if (job.isLineId) {
				q = sm.execute(polquery);
			}else
			{
				q = sm.execute(query);
			}
			
			q = q.setLong("poId", job.PId);
			
			List<PurchaseOrderLine> lpol = q.list();
			for (PurchaseOrderLine purchaseOrderLine : lpol) {
				purchaseOrderLine.getInventoryElement().getId();
				purchaseOrderLine.getInventoryElement().getSupplyResourceItem().getName();
				purchaseOrderLine.getInventoryElement().getSupplyResourceItem().getCategory().getName();
				
				if (purchaseOrderLine.getInventoryElement().getSupplyResourceItem().getUsageUnit() != null)
					purchaseOrderLine.getInventoryElement().getSupplyResourceItem().getUsageUnit().getName();
				
				if (purchaseOrderLine.getSupplyResourceLocation() != null){
				purchaseOrderLine.getSupplyResourceLocation().getId();
				
				purchaseOrderLine.getSupplyResourceLocation().getShortName();
				}
				purchaseOrderLine.getPurchaseOrder().getId();
				purchaseOrderLine.getPurchaseOrder().getPoNumber();	
				
				if (purchaseOrderLine.getReceiveOrderLines() != null){
					for(ReceiveOrderLine rol : purchaseOrderLine.getReceiveOrderLines()){
					 if (rol.getAdjustment() != null){
						 rol.getAdjustment().getId();
						 rol.getAdjustment().getQuantity();
					 }
					 if (rol.getReceiveOrder() != null){ //AES-10430
						 rol.getReceiveOrder().getReceivedDate();
						 rol.getReceiveOrder().getShippingNumber();
					 } //AES-10430
					}
				}
				
				if (purchaseOrderLine.getVendorOrderInformation() != null) 
				{
					purchaseOrderLine.getVendorOrderInformation().getId();
					purchaseOrderLine.getVendorOrderInformation().getVendor().getId();
					purchaseOrderLine.getVendorOrderInformation().getVendor().getDescription();
					purchaseOrderLine.getVendorOrderInformation().getPricePerOrderingUnit();
					purchaseOrderLine.getVendorOrderInformation().getOrderingUnit().getId();
					if (purchaseOrderLine.getVendorOrderInformation().getOrderingUnit() != null)
						purchaseOrderLine.getVendorOrderInformation().getOrderingUnit().getName();
					purchaseOrderLine.getVendorOrderInformation().getVendorDescription();
					purchaseOrderLine.getVendorOrderInformation().getVendorCatalog();
					purchaseOrderLine.getVendorOrderInformation().getManufacturerCatalog();
					purchaseOrderLine.getVendorOrderInformation().getManufacturer().getDescription();
					
				}	
				for (VendorOrderInformation vo :purchaseOrderLine.getInventoryElement().getVendorOrderInformations()) {
					vo.getId();
									}
				
				if (purchaseOrderLine.getInventoryElement().getItemLocations().size() > 0) {
					purchaseOrderLine.getInventoryElement().getItemLocations().iterator().next().getLocation().getName();
				}
			}
			return (ArrayList) lpol;
		} catch (Exception e) {
			throw e;
		} finally {
			sm.closeSession();
		}	
	}

}
